package com.no1ikun.view;

import com.no1ikun.service.StudentServiceImpl;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SelectJPanel extends JPanel {
    private JComboBox select;
    private JTable table;
    private String[] title = {"学号", "姓名", "性别","生日","籍贯"};
    private MyButton ssbut;

    private StudentServiceImpl ss = new StudentServiceImpl();

    public SelectJPanel() {
        //初始化数据
        init();
    }


    public void init() {
        this.setLayout(null);//绝对布局
        this.setBounds(80, 80, 885, 452);
        this.setBackground(Color.WHITE);
        MyJLabel time = new MyJLabel("选择你的操作", 200, 30, 120, 35, "微软雅黑", Font.BOLD, 17, new Color(28, 136, 201));
        select = new JComboBox();
        select.setBounds(250, 37, 150, 25);
        select.setBorder(null);
        select.setBackground(Color.WHITE);
        select.addItem("2023年级名单");
        select.addItem("1班名单");
        select.addItem("2班名单");
        select.addItem("3班名单");
        select.addItem("4班名单");


        //去数据库查询数据,班级信息

        String msg = ss.selectClassMsg();

        MyJLabel centerTest = new MyJLabel(msg, 600, 30, 50, 75, "微软雅黑", 0, 15, new Color(252, 89, 45));

        //添加搜索按钮
        ssbut = new MyButton("确定", 440, 37, 60, 25, new Color(28, 136, 201), Color.WHITE);


        //去数据库查询出数据
        Object[][] data = ss.getData();
        //要一个二维数组
        DefaultTableModel defatleTable = new DefaultTableModel(data, title){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        //定义一个表格
        table = new JTable(defatleTable);
        //设置行高
        table.setRowHeight(25);

        //创建一个渲染器
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        //内容居中显示
        dc.setHorizontalAlignment(JLabel.CENTER);

        table.setDefaultRenderer(Object.class, dc);
        //设置指定类的宽
        table.getColumnModel().getColumn(0).setPreferredWidth(100);
        table.getColumnModel().getColumn(2).setPreferredWidth(100);
        table.getColumnModel().getColumn(3).setPreferredWidth(100);
        //添加一个滚动条
        JScrollPane js = new JScrollPane(table);
        js.setBounds(10, 70, 865, 375);


        this.add(centerTest);
        this.add(ssbut);
        this.add(select);
        this.add(time);
        this.add(js);

        //监听搜索按钮的监听事件
        ssbut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.接收用户要检索的数据
                int text = select.getSelectedIndex();
                System.out.println("要选择是:" + text);
                String sql = "select sno,spwd,sname,sex,birthday,native_place,earth_place,s.cno,cname,dname from Student s JOIN  Class c on s.cno=c.cno ";
                switch (text) {
                    case 1:
                        sql = "select sno,spwd,sname,sex,birthday,native_place,earth_place,s.cno,cname,dname from Student s JOIN  Class c on s.cno=c.cno  where s.cno='C00001'";
                        break;
                    case 2:
                        sql = "select sno,spwd,sname,sex,birthday,native_place,earth_place,s.cno,cname,dname from Student s JOIN  Class c on s.cno=c.cno  where s.cno='C00002'";
                        break;
                    case 3:
                        sql = "select sno,spwd,sname,sex,birthday,native_place,earth_place,s.cno,cname,dname from Student s JOIN  Class c on s.cno=c.cno  where s.cno='C00003'";
                        break;
                    case 4:
                        sql = "select sno,spwd,sname,sex,birthday,native_place,earth_place,s.cno,cname,dname from Student s JOIN  Class c on s.cno=c.cno  where s.cno='C00004'";
                        break;
                }
                //去数据库查询
                Object[][] stus = ss.getObjects(sql);
                //重新完成数据渲染
                TableModel dataModel = new DefaultTableModel(stus, title);
                table.setModel(dataModel);


            }
        });


    }
}
